<?php

namespace App\Controllers\Admin;

use App\Controllers\AdminBase;
use App\Models\UploadsModel;

class Opi extends AdminBase
{


    //获取浏览统计
    public function getVisitsIplog()
    {

        $data = [];
        $time = time();

        //昨天
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        $yesterday_data = $this->db->where('day', $yesterday)->get('rank_info')->result_array();

        if ($yesterday_data) {
            foreach ($yesterday_data as $key => $val) {
                $data[$val['type']]['yesterday'] = $val['hits'];
            }
        }
        //今天
        $day = date('Ymd', $time);
        $day_data = $this->db->where('day', $day)->get('rank_info')->result_array();
        if ($day_data) {
            foreach ($day_data as $key => $val) {
                $data[$val['type']]['day'] = $val['hits'];
            }
        }
        //本周
        //$week_start = mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
        //$week_end  =  mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
        $week_start = mktime(23, 59, 59, date("m"), date("d") - 7, date("Y"));
        $week_data = $this->db->select('type')->where("inputtime between " . $week_start . " AND " . time())->select_sum('hits', 'tp_sum')->group_by('type')->get('rank_info')->row_array();

        if ($week_data) {
            foreach ($week_data as $key => $val) {
                $data[$val['type']]['week'] = $val['tp_sum'];
            }
        }
        //本月
        //$month_start = mktime(0, 0 , 0,date("m"),1,date("Y"));
        //$month_end  =  mktime(23,59,59,date("m"),date("t"),date("Y"));
        $month_start = mktime(23, 59, 59, date("m"), date("d") - 30, date("Y"));
        $month_data = $this->db->select('type')->where("inputtime between " . $month_start . " AND " . time())->select_sum('hits', 'tp_sum')->group_by('type')->get('rank_info')->row_array();

        if ($month_data) {
            foreach ($month_data as $key => $val) {
                $data[$val['type']]['month'] = $val['tp_sum'];
            }
        }
        //总数
        $views_data = $this->db->select('type')->group_by('type')->select_sum('hits', 'tp_sum')->get('rank_info')->result_array();

        if ($views_data) {
            foreach ($views_data as $key => $val) {
                $data[$val['type']]['views'] = $val['tp_sum'];
            }
        }

        $datas['data'] = [
            "pc_pv" => $data[2],
            "pc_ip" => $data[1],
            "mobile_pv" => $data[4],
            "mobile_ip" => $data[3],
        ];
        exit(json_encode($datas));
    }

    /**
     * 生成安全码
     */
    public function syskey()
    {
        $str = 'MOK' . mt_rand(111, 999) . strtoupper(substr((md5(time())), rand(0, 10), 13));
        $this->_json(0, 'ok', $str);
    }

    /**
     * 生成来路随机字符
     */
    public function referer()
    {
        $s = strtoupper(base64_encode(md5(time()) . md5(rand(0, 2015) . md5(rand(0, 2015)))) . md5(rand(0, 2009)));
        $str = str_replace('=', '', substr($s, 0, 64));
        $this->_json(0, 'ok', $str);
    }


    //上传单个文件
    public function upload()
    {
        if (IS_POST || IS_AJAX_POST) {
            $file = $this->request->getFile('file');
            $postdata = $this->request->getPost();

            //ip
            $ip = $this->request->getIPAddress();
            //文件大小
            $size = $file->getSize();
            //文件后缀名没有.
            $ext = $file->getExtension();
            //文件类型
            $type = $file->getType();
            //文件名称
            $name = $file->getName();
            //mime
            $mime = $file->getMimeType();
            //临时文件目录
            $tempfile = $file->getTempName();
            //实际存储目录
            $dir = '/writable/uploads/' . date('Ym') . '/' . date('d') . '/';
            //重命名
            $newName = $file->getRandomName();
            $nfile = $dir . $newName;

            if (!in_array($ext, ['png', 'jpg', 'jpeg', 'gif', 'bmp'])) {
                $this->_json(1, '上传失败', '错误:请上传图片');
            }

            //移动文件
            if ($file->isValid() && !$file->hasMoved()) {
                $file->move(WEBPATH . $dir, $newName);
            } elseif (!$file->isValid()) {
                if ($file->getErrorString()) {
                    $this->_json(1, '上传失败', '错误:' . $file->getError());
                }
            }

            if ($file->hasMoved()) {
                $info = getimagesize(WEBPATH . $nfile);
                $thumb = '/writable/assets/file.png';
                if ($ext === 'png' || $ext === 'jpg' || $ext === 'jpeg' || $ext === 'gif') {
                    $thumb = thumb($nfile, 200, 200);
                }
                $data = [
                    'filename' => $name,
                    'fileext' => $ext,
                    'filesize' => $size,
                    'attachment' => $nfile,
                    'inputtime' => time(),
                    'fileinfo' => json_encode($info),
                    'adminid' => 0,
                    'filename2' => $newName,
                ];
                $this->uploadsModel = new UploadsModel();
                $rt = $this->uploadsModel->add($data);
                if ($rt) {
                    $rt['data'] = ['thumb' => $thumb, 'src' => SITE_URL . trim($nfile, '/'), 'fileid' => $rt['data'], 'title' => $name, 'size' => $size, 'ext' => $ext];
                }
                $this->_json($rt);
            } else {
                //移动失败
                $data = ['src' => $nfile, 'title' => $name, 'size' => $size, 'ext' => $ext];
                $this->_json(1, '上传失败', $data);
            }
        }
        $this->_json(1, '上传失败:POST');
    }
}
